package swc.smu.ui.custom;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by smu on 2016/2/4.
 */
public class SelectImageView extends View {
    final static int MARGIN_SIDE = 200;
    Rect centerRect = new Rect();
    static Paint paint = new Paint();
    int width, height, middleSize;
    int minX, minY, maxX, maxY;
    float touchX, touchY;
    Context context;

    public SelectImageView(Context context) {
        super(context);
        this.context = context;
    }

    public SelectImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public SelectImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setAntiAlias(true);
        if(!(context instanceof Activity)) {
            return;
        }
        int actionBarHeight;
        try {
            actionBarHeight = ((AppCompatActivity)context).getSupportActionBar().getHeight();
        } catch (Exception e) {
            try {
                actionBarHeight = ((Activity)context).getActionBar().getHeight();
            } catch (Exception ex) {
                actionBarHeight = 0;
            }
        }

//        canvas.drawARGB(128, 128, 128, 128); //背景朦层
//        canvas.drawARGB(153, 255, 0, 0);

        width = canvas.getWidth();
        height = canvas.getHeight();

        middleSize = width > height ? height : width;
        double d = (double)(width * width + height * height);
        int paintStrockWidth = ((int)Math.ceil(Math.sqrt(d) / 2))  + MARGIN_SIDE;

        float left = (width - middleSize) / 2 + MARGIN_SIDE - paintStrockWidth / 2;
        float top = (height - middleSize) / 2 + MARGIN_SIDE - actionBarHeight / 2 - paintStrockWidth / 2;
        float right = width / 2 + middleSize / 2 - MARGIN_SIDE + paintStrockWidth / 2;
        float bottom = height / 2 +  middleSize / 2 - MARGIN_SIDE + paintStrockWidth / 2;

        minX = (int) left + paintStrockWidth / 2;
        minY = (int) top + paintStrockWidth / 2;
        maxX = (int) right - paintStrockWidth / 2;
        maxY = (int) bottom - paintStrockWidth / 2;
        centerRect.set(minX, minY, maxX, maxY);
//        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        paint.setColor(Color.argb(230, 40, 40, 40));
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(paintStrockWidth);

        RectF f = new RectF(left, top, right, bottom);
        canvas.drawOval(f, paint); //绘制椭圆
//        canvas.drawArc(f, 0, 360, false, paint); //绘制圆弧
//        canvas.drawArc(left, top, right, bottom, 30, 180, true, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        touchX = event.getX();
        touchY = event.getY();
        boolean isTouch = isTouch(touchX, touchY);
        if(isTouch) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 判断触摸区域是否在可用区内
     * @param x
     * @param y
     * @return
     */
    public boolean isTouch(float x, float y) {
        if(x >= minX && x <= maxX && y >= minY && y <= maxY) {
            return true;
        }
        return  false;
    }

    /**
     * 返回中心区域的矩形
     * @return
     */
    public Rect getCenterRect() {
        return centerRect;
    }
}
